class Solution:
    def minInsertions(self, s: str) -> int:
        # 插入一个对应着删除一个
        # 即找到最长回文子序列
        n = len(s)
        dp = [[0 for _ in range(n)] for _ in range(n)]
        for i in range(n):
            dp[i][i] = 1
        for i in range(n - 1, -1, -1):
            for j in range(i + 1, n):
                if s[i] == s[j]:
                    dp[i][j] = dp[i + 1][j - 1] + 2
                else:
                    dp[i][j] = max(dp[i + 1][j], dp[i][j - 1])
        return n - dp[0][-1]


if __name__ == '__main__':
    s = Solution()
    r = s.minInsertions("mbadm")
    print(r)
